<?php if(!defined('CALL')) exit('No direct script access allowed');

class LocaleStrings
{
	private $lang = 'ru';
	private $theme = '';
	private $x = null;
	
	public function __construct($lang, $theme)
	{
		$this->lang = strtolower($lang);
		$this->theme = $theme;
		$this->x = simplexml_load_file('locales/'.$lang.'-'.$theme.'.xml');
	}
	
	public function gets($tag, $code = null)
	{
		$str = "";
		$nodes = null;
		
		if(is_null($code))
			$nodes = $this->x->xpath($tag);
		else
			$nodes = $this->x->xpath($tag."[@code='".$code."']");
		
		foreach($nodes as $node)
			$str .= "\n".$node['message'];
			
		if(count($nodes) > 0) $str = substr($str, 1);
		
		return $str;
	}
	
	public function get($tag, $code, $assoc = null)
	{
		$str = "";
		$nodes = null;
		
		$nodes = $this->x->xpath($tag."[@code='".$code."']");
		
		if(count($nodes) == 0) return "";
		
		$str = $nodes[0]['message'];
		
		
		if(is_null($assoc) || !is_array($assoc)) return $str;
		
		$sour = array();
		$dest = array();
		foreach($assoc as $key => $val)
		{
			$sour[] = '{'.$key.'}';
			$dest[] = $val;
		}
		
		return str_replace($sour, $dest, $str);
	}
	
}